package com.example.chapter02;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener ,View.OnFocusChangeListener{

    //声明我们要用到的按钮button
    private Button btn_login,btn_register;
    private EditText ed_name,ed_psd;
    String name,psd,error;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("tag","onCreate");
        System.out.println("onCreate");
        initView();
    }

    //初始化控件
    public void initView(){
        btn_login=findViewById(R.id.button_login);
        btn_register=findViewById(R.id.button_register);
        ed_name =findViewById(R.id.edit_username);
        ed_psd=findViewById(R.id.edit_password);


        name=ed_name.getText().toString();
        psd=ed_psd.getText().toString();

        //ed_name.setOnFocusChangeListener();
        ed_psd.setOnFocusChangeListener(this);


        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);

       /* btn_login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast.makeText(MainActivity.this,"登录",Toast.LENGTH_LONG).show();
            }
        });*/

    }

    @Override
    public void onClick(View v) {
        psd=ed_psd.getText().toString();
        name=ed_name.getText().toString();
        switch (v.getId()){
            case R.id.button_login:
                //Toast.makeText(MainActivity.this,name,Toast.LENGTH_LONG).show();
                if(name.isEmpty()||name==null){
                    error="姓名不可为空";
                }
                else if(psd.isEmpty()||psd==null){
                    error="密码不可为空";
                }
                else if(!name.equals("admin")){
                    error="用户名不正确";
                }
                else if(!psd.equals("123456")){
                    error="密码不正确";
                }
                else if(name.equals("admin")&&psd.equals("123456")){
                    error="登录成功";
                }
                Toast.makeText(MainActivity.this,error,Toast.LENGTH_LONG).show();
                Intent intent=new Intent(MainActivity.this,UserActivity.class);
                intent.putExtra("username","admin");
                startActivity(intent);
                finish();
                break;
            case R.id.button_register:
                intent=new Intent(MainActivity.this,reg.class);
                startActivityForResult(intent,101);
                break;
        }


    }

    //处理返回数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       // super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==101&&resultCode==102){
            Bundle bundle=data.getBundleExtra("bundle");
            //ed_name.setText( bundle.getString("username"));
           // ed_psd.setText( bundle.getString("pwd"));
        }
        Log.i("tag","处理返回数据");

    }

    //状态测试


    @Override
    protected void onRestart() {
        System.out.println("onRestart");
        Log.i("tag","onRestart");
        super.onRestart();
    }

    @Override
    protected void onPause() {
        System.out.println("onPause");
        Log.i("tag","onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        System.out.println("onStop");
        Log.i("tag","onStop");
        super.onStop();
    }

    @Override
    protected void onResume() {
        Log.i("tag","onResume");
        User user=new User();
        user.getUser(this);
        ed_name.setText( user.getName());
        ed_psd.setText( user.getPwd());
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        Log.i("tag","onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        System.out.println("onStart");
        Log.i("tag","onStart");
        super.onStart();
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            ed_psd.setHint(null);
        }else{
            ed_psd.setHint("password22222");
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
